package oa.hleast.xswl.controller.app.article;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.constant.ConfigKeyConstant;
import oa.hleast.common.result.Result;
import oa.hleast.common.result.ResultCode;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.common.constant.CommonConstant;
import oa.hleast.xswl.pojo.domain.SysDictItem;
import oa.hleast.xswl.pojo.domain.article.CmsArticle;
import oa.hleast.xswl.pojo.form.article.CmsArticleForm;
import oa.hleast.xswl.pojo.vo.DictItemValueByUserVO;
import oa.hleast.xswl.pojo.vo.article.CmsArticleVo;
import oa.hleast.xswl.pojo.vo.article.ShowArticleVo;
import oa.hleast.xswl.service.ISysCommonConfigService;
import oa.hleast.xswl.service.ISysDictItemService;
import oa.hleast.xswl.service.IUmsUserService;
import oa.hleast.xswl.service.article.IInformationCentreService;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 302
 * @description
 * @date 2021/3/23
 */
@Api(tags = "【移动端】资讯中心")
@RestController
@RequestMapping("/api.app/v1/information")
@AllArgsConstructor
@Slf4j
public class InformationCentreController {

    private final IInformationCentreService informationCentreService;
    private final ISysCommonConfigService commonConfigService;
    private final ISysDictItemService dictItemService;
    private final IUmsUserService umsUserService;

    @ApiOperation(value = "列表分页-缓存", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", dataType = "Integer",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Integer",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "id", value = "资讯类型id", paramType = "query", dataType = "String",dataTypeClass = String.class)
    })
    @GetMapping("find-page-show-list")
    public Result<List<ShowArticleVo>> findPageShowList(
            @RequestParam @NotNull Integer page,
            @RequestParam @NotNull Integer limit,
            @RequestParam @NotNull String id
    ) {
        log.info("page=>{},limit=>{},ID=>{}", page, limit, id);
        Page<ShowArticleVo> result = informationCentreService.findPageShowList(page, limit, id);
        return Result.success(result.getRecords(), result.getTotal());
    }


    @ApiOperation(value = "列表分页-一码通热门推荐", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", dataType = "Integer",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Integer",dataTypeClass = Integer.class)
    })
    @GetMapping("findPageHotRecommendList")
    public Result<List<ShowArticleVo>> findPageHotRecommendList(
            @RequestParam @NotNull Integer page,
            @RequestParam @NotNull Integer limit
    ) {
        log.info("page=>{},limit=>{}", page, limit);
        Page<ShowArticleVo> result = informationCentreService.findPageHotRecommendList(page, limit);
        return Result.success(result.getRecords(), result.getTotal());
    }

    @ApiOperation(value = "table-列表", httpMethod = "GET")
    @GetMapping("findInformationType")
    public Result<List<SysDictItem>> findInformationType() {
        List<SysDictItem> result = informationCentreService.findInformationType();

        return Result.success(result);
    }

    @ApiOperation(value = "信息头条分类带用户选中状态", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "showAll", value = "是否显示所有分类， 如果用户没有登录必须被true", paramType = "query", dataType = "Boolean",dataTypeClass = Boolean.class)
    })
    @GetMapping("findInformationTypeByUser")
    public Result<List<DictItemValueByUserVO>> findInformationTypeByUser(boolean showAll) {

        Long userId = RequestUtils.getUserId();
        if (ObjectUtil.isNull(userId) && !showAll) {
            return Result.failed(ResultCode.PARAM_ERROR);
        }

        List<DictItemValueByUserVO> listTemp = informationCentreService.findInformationTypeByUser(userId, showAll);

        Long introductionArticleId = 0L;
        try {
            String value = commonConfigService.getByConfigKey(ConfigKeyConstant.INTRODUCTION_ARTICLE_ITEM_ID).getValue();
            introductionArticleId = new Long(value);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("e = {}", e, e);
        }

        Long finalIntroductionArticleId = introductionArticleId;
        List<DictItemValueByUserVO> list = listTemp.stream().filter(dictItem -> !dictItem.getId().equals(finalIntroductionArticleId)).collect(Collectors.toList());


        // 添加推荐table
        list.add(0, new DictItemValueByUserVO(CommonConstant.INFORMATION_COMMEND_ID, "推荐", "commend", -1));
        return Result.success(list);
    }


    @ApiOperation(value = "资讯详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/getDetail")
    public Result<CmsArticleVo> getDetail(@RequestParam @NotNull Long id) {
        Long userId = RequestUtils.getUserId();
        CmsArticleVo vo = informationCentreService.getCacheDetail(id, userId);
        return Result.success(vo);
    }

    @ApiOperation(value = "判断是否存在某条数据 -供其他服务调用", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/countInformationById")
    public Result<Integer> countById(@RequestParam @NotNull Long id) {
        Integer count = informationCentreService.countById(id);
        return Result.success(count);
    }

    @ApiOperation(value = "通过配置key获取资讯详情", httpMethod = "GET")
    @ApiImplicitParam(name = "configKey", value = "configKey", paramType = "query", dataType = "String",dataTypeClass = String.class)
    @GetMapping("/getDetailByConfigKey")
    public Result<CmsArticleVo> getDetailByConfigKey(@RequestParam @NotNull String configKey) {
        Long userId = RequestUtils.getUserId();
        CmsArticleVo vo = informationCentreService.getDetailByConfigKey(configKey, userId);
        return Result.success(vo);
    }

    @ApiOperation(value = "批量通过配置key获取资讯详情", httpMethod = "GET")
    @ApiImplicitParam(name = "configKeyList", value = "configKeyList", paramType = "query", dataType = "List",dataTypeClass = List.class)
    @GetMapping("/findDetailByConfigKey")
    public Result<List<CmsArticleVo>> findDetailByConfigKey(@RequestParam @NotNull List<String> configKeyList) {
        Long userId = RequestUtils.getUserId();
        List<CmsArticleVo> voList = informationCentreService.findDetailByConfigKey(configKeyList, userId);
        return Result.success(voList);
    }


    @ApiOperation(value = "公众号资讯是否同步过", httpMethod = "GET")
    @ApiImplicitParam(name = "aid", value = "公众号资讯ID", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @GetMapping("isInformationExist")
    public Result<Integer> isInformationExist(@RequestParam String aid) {
        int count = informationCentreService.count(new LambdaQueryWrapper<CmsArticle>().eq(CmsArticle::getAid, aid));
        return Result.success(count);
    }

    @ApiOperation(value = "新增资讯", httpMethod = "POST")
    @ApiImplicitParam(name = "articleForm", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsArticleForm",dataTypeClass = CmsArticleForm.class)
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody @NotNull CmsArticleForm articleForm) {
        boolean status = informationCentreService.add(articleForm);
        return Result.judge(status);
    }
}
